import request from '@/utils/request'

// 获取聊天会话列表
export function getChatList() {
  return request({
    url: '/chat/sessions',
    method: 'get'
  })
}

// 获取聊天历史记录
export function getChatHistory(appointmentId: number) {
  return request({
    url: `/chat/history/${appointmentId}`,
    method: 'get'
  })
}

// 发送消息
export function sendMessage(data: {
  appointmentId: number
  content: string
  contentType: 'TEXT' | 'IMAGE' | 'FILE'
  receiverId: number
  fileUrl?: string
  fileName?: string
}) {
  return request({
    url: '/chat.send',
    method: 'post',
    data
  })
}

// 上传文件
export function uploadFile(file: File) {
  const formData = new FormData()
  formData.append('file', file)
  return request({
    url: '/chat/upload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 获取或创建聊天会话
 * @param appointmentId 预约ID
 */
export function getChatSession(appointmentId: number) {
  return request({
    url: `/chat/session/${appointmentId}`,
    method: 'get'
  })
}

// 标记消息为已读
export function markMessagesAsRead(appointmentId: number) {
  return request({
    url: `/chat/read/${appointmentId}`,
    method: 'post'
  })
}

// 添加撤回消息的API
export function recallMessage(messageId: number) {
  return request({
    url: `/chat/message/${messageId}/recall`,
    method: 'post'
  })
}

/**
 * 结束聊天会话
 * @param appointmentId 预约ID
 */
export function endChatSession(appointmentId: number) {
  return request({
    url: `/chat/session/${appointmentId}/end`,
    method: 'post'
  })
} 